package com.renge.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.renge.commonutils.R;
import com.renge.eduservice.client.VodClient;
import com.renge.eduservice.entity.EduVideo;
import com.renge.eduservice.mapper.EduVideoMapper;
import com.renge.eduservice.service.EduVideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.renge.exception.GuliException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-06-04
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {
    @Autowired
    private VodClient vodClient;
    @Override
    @Transactional
    public boolean deleteById(String id) {
        //查询是否存在该id的
        QueryWrapper<EduVideo> eduVideoWrapper = new QueryWrapper<>();
        eduVideoWrapper.eq("id",id);
        EduVideo one = this.getOne(eduVideoWrapper);
        //若不存在，则抛异常
        if(one==null) {
            GuliException guliException = new GuliException();
            guliException.setCode(250);
            guliException.setMsg("删除失败，不存在此小节");
            throw guliException;
        }
        //得到视频id，删除阿里云上的视频
        String videoSourceId = one.getVideoSourceId();
        if(!StringUtils.isEmpty(videoSourceId)){
            //远程方法删除
            R r = vodClient.deleteVideoById(videoSourceId);
            //判断是否调用回调方法
            if(r.getCode()==20001){
                GuliException guliException = new GuliException();
                guliException.setMsg("删除视频错误");
                this.removeById(id);
                throw guliException;
            }
        }
        //执行删除操作
        return this.removeById(id);
    }
}
